Skip to content

Method: buildAxiomValuesFromInstance(Object, AxiomValueGatherer)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.oom;
19:
20: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
21: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
22: import cz.cvut.kbss.jopa.model.metamodel.ListAttributeImpl;
23: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
24: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
25: import cz.cvut.kbss.ontodriver.descriptor.ListDescriptor;
26: import cz.cvut.kbss.ontodriver.descriptor.ListValueDescriptor;
27: import cz.cvut.kbss.ontodriver.model.NamedResource;
28:
29: import java.util.Collections;
30: import java.util.List;
31: import java.util.Objects;
32: import java.util.stream.Collectors;
33:
34: abstract class ListPropertyStrategy<L extends ListDescriptor, V extends ListValueDescriptor, X>
35: extends PluralObjectPropertyStrategy<ListAttributeImpl<? super X, ?>, X> {
36:
37: ListPropertyStrategy(EntityType<X> et, ListAttributeImpl<? super X, ?> att, Descriptor descriptor,
38: EntityMappingHelper mapper) {
39: super(et, att, descriptor, mapper);
40: }
41:
42: @Override
43: protected void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) {
44: final Object value = extractFieldValueFromInstance(instance);
45:• assert value instanceof List || value == null;
46: extractListValues((List<?>) value, instance, valueBuilder);
47: }
48:
49: /**
50: * Adds elements of the list to the value descriptor.
51: */
52: <K> void addListElementsToListValueDescriptor(ListValueDescriptor listDescriptor, List<K> list) {
53: if (list == null) {
54: return;
55: }
56: final Class<?> elemType = attribute.getBindableJavaType();
57: if (IdentifierTransformer.isValidIdentifierType(elemType)) {
58: list.stream().filter(Objects::nonNull)
59: .forEach(item -> listDescriptor.addValue(NamedResource.create(IdentifierTransformer.valueAsUri(item))));
60: } else if (elemType.isEnum()) {
61: assert attribute.getConverter() != null;
62: list.stream().filter(Objects::nonNull)
63: .forEach(item -> listDescriptor.addValue(attribute.getConverter().convertToAxiomValue(item)));
64: } else {
65: final EntityType<?> valueType = mapper.getEntityType(elemType);
66: addIndividualsToDescriptor(listDescriptor, list, valueType);
67: }
68: }
69:
70: static void addIndividualsToDescriptor(ListValueDescriptor listDescriptor, List<?> list, EntityType<?> valueType) {
71: list.stream().filter(Objects::nonNull).forEach(item -> listDescriptor
72: .addValue(NamedResource.create(EntityPropertiesUtils.getIdentifier(item, valueType))));
73: }
74:
75: <K> List<K> resolveUnpersistedItems(List<K> list) {
76: assert attribute.isAssociation();
77: if (list == null || IdentifierTransformer.isValidIdentifierType(attribute.getBindableJavaType()) || attribute.getBindableJavaType()
78: .isEnum()) {
79: return Collections.emptyList();
80: } else {
81: return list.stream().filter(item -> item != null && !referenceSavingResolver
82: .shouldSaveReferenceToItem(item, getAttributeValueContexts())).collect(Collectors.toList());
83: }
84: }
85:
86: abstract <K> void extractListValues(List<K> list, X instance, AxiomValueGatherer valueBuilder);
87: }